### REPLICATION 
### RD plots for business_mayors
### this script cleans & bins data for RD plots


# mayoral & fiscal data 
load("replication_data_business_mayors_RD.RData")
mayors_subs <- subset(mayors, !is.na(exec_margin))



### Revenue


#### bin data
bin.size <- 0.025

# Number of bins on each side of cutoff
numbelow <- length(seq(min(mayors_subs$exec_margin), 0, bin.size))
numabove <- length(seq(0, max(mayors_subs$exec_margin), bin.size))

# Create bin identifiers -- add 1000 (or some large number) to the ones above
# in order to distinguish from those that are below
b1 <- cut(mayors_subs$exec_margin[mayors_subs$exec_margin < 0], numbelow, labels=FALSE)
b2 <- 1000 + cut(mayors_subs$exec_margin[mayors_subs$exec_margin >= 0], numabove, labels=FALSE)
###add 1000 (or something) to b2 so that bin labels are not the same on both sides of cutpoint

# Preprocess data
dat.below.revs <- cbind(b1, mayors_subs$exec_margin[mayors_subs$exec_margin < 0], mayors_subs$pc_ttl_revs_lead2[mayors_subs$exec_margin < 0])
dat.above.revs <- cbind(b2, mayors_subs$exec_margin[mayors_subs$exec_margin >= 0], mayors_subs$pc_ttl_revs_lead2[mayors_subs$exec_margin >= 0])
dat.binned.revs <- as.data.frame(rbind(dat.below.revs, dat.above.revs))
names(dat.binned.revs) <- c("bin","z","y")


binned_revenue <- 
  dat.binned.revs %>% 
  group_by(bin) %>% 
  summarise(bin_count = n(),
            binned_rev = mean(y, na.rm = TRUE),
            binned_margin = mean(z, na.rm = TRUE))





### Expenditures


#### bin data
bin.size <- 0.025

# Number of bins on each side of cutoff
numbelow <- length(seq(min(mayors_subs$exec_margin), 0, bin.size))
numabove <- length(seq(0, max(mayors_subs$exec_margin), bin.size))

# Create bin identifiers -- add 1000 (or some large number) to the ones above
# in order to distinguish from those that are below
b1 <- cut(mayors_subs$exec_margin[mayors_subs$exec_margin < 0], numbelow, labels=FALSE)
b2 <- 1000 + cut(mayors_subs$exec_margin[mayors_subs$exec_margin >= 0], numabove, labels=FALSE)
###add 1000 (or something) to b2 so that bin labels are not the same on both sides of cutpoint

# Preprocess your data

dat.below.exps <- cbind(b1, mayors_subs$exec_margin[mayors_subs$exec_margin < 0], mayors_subs$pc_ttl_exps_lead2[mayors_subs$exec_margin < 0])
dat.above.exps <- cbind(b2, mayors_subs$exec_margin[mayors_subs$exec_margin >= 0], mayors_subs$pc_ttl_exps_lead2[mayors_subs$exec_margin >= 0])
dat.binned.exps <- as.data.frame(rbind(dat.below.exps, dat.above.exps))
names(dat.binned.exps) <- c("bin","z","y")


binned_expenditure <- 
  dat.binned.exps %>% 
  group_by(bin) %>% 
  summarise(bin_count = n(),
            binned_exp = mean(y, na.rm = TRUE),
            binned_margin = mean(z, na.rm = TRUE))





### Roads


#### bin data
bin.size <- 0.02

# Number of bins on each side of cutoff
numbelow <- length(seq(min(mayors_subs$exec_margin), 0, bin.size))
numabove <- length(seq(0, max(mayors_subs$exec_margin), bin.size))

# Create bin identifiers -- add 1000 (or some large number) to the ones above
# in order to distinguish from those that are below
b1 <- cut(mayors_subs$exec_margin[mayors_subs$exec_margin < 0], numbelow, labels=FALSE)
b2 <- 1000 + cut(mayors_subs$exec_margin[mayors_subs$exec_margin >= 0], numabove, labels=FALSE)
###add 1000 (or something) to b2 so that bin labels are not the same on both sides of cutpoint

# Preprocess your data

dat.below.roads <- cbind(b1, mayors_subs$exec_margin[mayors_subs$exec_margin < 0], mayors_subs$pc_roads_lead2[mayors_subs$exec_margin < 0])
dat.above.roads <- cbind(b2, mayors_subs$exec_margin[mayors_subs$exec_margin >= 0], mayors_subs$pc_roads_lead2[mayors_subs$exec_margin >= 0])
dat.binned.roads <- as.data.frame(rbind(dat.below.roads, dat.above.roads))
names(dat.binned.roads) <- c("bin","z","y")



binned_roads <- 
  dat.binned.roads %>% 
  group_by(bin) %>% 
  summarise(bin_count = n(),
            binned_roads = mean(y, na.rm = TRUE),
            binned_margin = mean(z, na.rm = TRUE))



### Housing


#### bin data
bin.size <- 0.02

# Number of bins on each side of cutoff
numbelow <- length(seq(min(mayors_subs$exec_margin), 0, bin.size))
numabove <- length(seq(0, max(mayors_subs$exec_margin), bin.size))

# Create bin identifiers -- add 1000 (or some large number) to the ones above
# in order to distinguish from those that are below
b1 <- cut(mayors_subs$exec_margin[mayors_subs$exec_margin < 0], numbelow, labels=FALSE)
b2 <- 1000 + cut(mayors_subs$exec_margin[mayors_subs$exec_margin >= 0], numabove, labels=FALSE)
###add 1000 (or something) to b2 so that bin labels are not the same on both sides of cutpoint

# Preprocess your data

dat.below.housing <- cbind(b1, mayors_subs$exec_margin[mayors_subs$exec_margin < 0], mayors_subs$pc_housing_lead2[mayors_subs$exec_margin < 0])
dat.above.housing <- cbind(b2, mayors_subs$exec_margin[mayors_subs$exec_margin >= 0], mayors_subs$pc_housing_lead2[mayors_subs$exec_margin >= 0])
dat.binned.housing <- as.data.frame(rbind(dat.below.housing, dat.above.housing))
names(dat.binned.housing) <- c("bin","z","y")

binned_housing <- 
  dat.binned.housing %>% 
  group_by(bin) %>% 
  summarise(bin_count = n(),
            binned_housing = mean(y, na.rm = TRUE),
            binned_margin = mean(z, na.rm = TRUE))


